package com.kexio.email.entity;

/**
 * 邮件附件实体
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public class EmailAttachment {
    
    /**
     * 附件名称
     */
    private String name;
    
    /**
     * 附件内容类型 (MIME Type)
     */
    private String contentType;
    
    /**
     * 附件大小 (字节)
     */
    private Long size;
    
    /**
     * 附件内容 (byte数组)
     */
    private byte[] content;
    
    /**
     * 附件文件路径 (如果是文件路径方式)
     */
    private String filePath;
    
    /**
     * 是否内联附件 (用于嵌入图片等)
     */
    private Boolean inline;
    
    /**
     * 内容ID (用于内联附件引用)
     */
    private String contentId;
    
    /**
     * 附件描述
     */
    private String description;
    
    // Constructors
    public EmailAttachment() {}
    
    public EmailAttachment(String name, String contentType, byte[] content) {
        this.name = name;
        this.contentType = contentType;
        this.content = content;
        this.size = content != null ? (long) content.length : 0L;
        this.inline = false;
    }
    
    public EmailAttachment(String name, String filePath) {
        this.name = name;
        this.filePath = filePath;
        this.inline = false;
    }
    
    public EmailAttachment(String name, String contentType, byte[] content, boolean inline, String contentId) {
        this.name = name;
        this.contentType = contentType;
        this.content = content;
        this.size = content != null ? (long) content.length : 0L;
        this.inline = inline;
        this.contentId = contentId;
    }
    
    /**
     * 检查是否为有效附件
     */
    public boolean isValid() {
        return (name != null && !name.trim().isEmpty()) && 
               ((content != null && content.length > 0) || 
                (filePath != null && !filePath.trim().isEmpty()));
    }
    
    /**
     * 获取附件大小的友好显示
     */
    public String getFormattedSize() {
        if (size == null || size == 0) {
            return "0 B";
        }
        
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double fileSize = size.doubleValue();
        
        while (fileSize >= 1024 && unitIndex < units.length - 1) {
            fileSize /= 1024;
            unitIndex++;
        }
        
        return String.format("%.1f %s", fileSize, units[unitIndex]);
    }
    
    // Getters and Setters
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    
    public String getContentType() { return contentType; }
    public void setContentType(String contentType) { this.contentType = contentType; }
    
    public Long getSize() { return size; }
    public void setSize(Long size) { this.size = size; }
    
    public byte[] getContent() { return content; }
    public void setContent(byte[] content) { 
        this.content = content; 
        this.size = content != null ? (long) content.length : 0L;
    }
    
    public String getFilePath() { return filePath; }
    public void setFilePath(String filePath) { this.filePath = filePath; }
    
    public Boolean getInline() { return inline; }
    public void setInline(Boolean inline) { this.inline = inline; }
    
    public String getContentId() { return contentId; }
    public void setContentId(String contentId) { this.contentId = contentId; }
    
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
}
